package com.javaee.web.servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javaee.ejb.entity.Employee;
import com.javaee.ejb.session.remote.EmployeeManagerRemote;
import com.javaee.web.HttpServletBase;
import com.javaee.web.SiteParameters;

/**
 * Servlet implementation class AddEmployeeServlet
 */
public class AddEmployeeServlet extends HttpServletBase {
	private static final long serialVersionUID = 1L;

	@EJB
	private EmployeeManagerRemote employeeManager;

	/**
	 * @see HttpServletBase#HttpServletBase()
	 */
	public AddEmployeeServlet() {
		super();
	}

	@Override
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String userName = null;
		String firstName = null;
		String lastName = null;
		String email = null;
		Date hireDate = null;
		int age = 0;

		// Verification des valeurs transmises
		HashMap<String, Boolean> hashInvalid = new HashMap<String, Boolean>();
		HashMap<String, String> hashValue = new HashMap<String, String>();

		hashValue.put("userName", request.getParameter("userName"));
		hashValue.put("firstName", request.getParameter("firstName"));
		hashValue.put("lastName", request.getParameter("lastName"));
		hashValue.put("email", request.getParameter("email"));
		hashValue.put("age", request.getParameter("age"));
		hashValue.put("hireDate", request.getParameter("hireDate"));

		userName = request.getParameter("userName");

		if (employeeManager.getByUserName(userName) != null) {
			hashInvalid.put("userName", new Boolean(false));
		}

		try {
			hireDate = new SimpleDateFormat("dd/MM/yyyy").parse(request
					.getParameter("hireDate"));
		} catch (Exception e) {
			hashInvalid.put("hireDate", new Boolean(false));
		}

		try {
			age = Integer.parseInt(request.getParameter("age"));
		} catch (Exception e) {
			hashInvalid.put("age", new Boolean(false));
		}

		if (hashInvalid.isEmpty() == false) {
			// Le formulaire est invalide
			request.setAttribute("hashInvalid", hashInvalid);
			request.setAttribute("hashValue", hashValue);
			request.setAttribute("formInvalid", "employeeForm");

			loadJSP(SiteParameters.getServletPath("administration"), request,
					response);
			return;
		} else {
			firstName = request.getParameter("firstName");
			lastName = request.getParameter("lastName");
			email = request.getParameter("email");

			Employee employee = new Employee();
			employee.setUserName(userName);
			employee.setFirstName(firstName);
			employee.setLastName(lastName);
			employee.setEmail(email);
			employee.setAge(age);
			employee.setHireDate(hireDate);
			employee.setIsAdmin(false);

			// Sauvegarde de l'employe
			employeeManager.saveEmployee(employee);

			// Redirection vers la page d'administration
			response.sendRedirect(SiteParameters
					.getFullServletPath("administration"));
		}
	}
}
